import java.text.DecimalFormat;
import java.util.Random;

import Jama.Matrix;


public class CyclicBarrierTest {

	public static void main(String[] args) {
		//double[][] vals = {{1.,2.,3., 4.5},{4.,5.,6., 8.5},{7.,8.,10., 10.5}, {12.0, 13.0, 14.0, 14.5}};
		
        Random random = new Random();

        double[][] vals = new double[1000][1000];

        for( int i = 0 ; i < vals.length ; i++ ) { 
           for ( int j = 0 ; j < vals[i].length ; j++ ) { 
        	   vals[i][j] = random.nextDouble();
           }
        }
	      Matrix A = new Matrix(vals);
	      Matrix b = Matrix.random(A.getColumnDimension(),1);
	      Matrix x = A.solve(b);
	      Matrix r = A.times(x).minus(b);
	      double rnorm = r.normInf();
	      System.out.print("\t A = ");
	      A.print(new DecimalFormat(), 4);
	      System.out.print("\t b = ");
	      b.print(new DecimalFormat(), 4);
	      System.out.print("\t x = ");
	      x.print(new DecimalFormat(), 4);
	      
	      Matrix AA = new Matrix(A.getRowDimension()*2,A.getColumnDimension()*2);
	      AA.setMatrix(0, A.getRowDimension()-1, 0, A.getColumnDimension()-1, A);
	      Matrix Aa = new Matrix(vals);
	      AA.setMatrix(A.getRowDimension(), AA.getRowDimension()-1, A.getColumnDimension(), AA.getColumnDimension()-1, Aa);
	      System.out.print("\t AA = ");
	      AA.print(new DecimalFormat(), 4);
	      
	      Matrix bb = new Matrix(b.getRowDimension()*2, 1);	      
	      bb.setMatrix(0, b.getRowDimension()-1, 0, b.getColumnDimension()-1, b);
	      Matrix b2 = b.copy();

	      bb.setMatrix(b2.getRowDimension(), bb.getRowDimension()-1, 0, 0, b2);
	      System.out.print("\t bb = ");
	      bb.print(new DecimalFormat(), 4);
	      double startTime = System.currentTimeMillis();
	      Matrix xx = AA.solve(bb);	      
	      System.out.print("Time (msec) taken to solve "+(System.currentTimeMillis()-startTime)+"\t xx = ");
	      //xx.print(new DecimalFormat(), 4);
	      
	      Matrix x2 = Aa.solve(b2);
	      
	      Matrix z = x2.minus(x);
	      
	      System.out.println("rnorm = "+rnorm);
	      z.print(new DecimalFormat(), 4);
	}
}
